package com.ims.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ims.dao.PrivilegeMapper;
import com.ims.pojo.Privilege;
import com.ims.service.PrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yekka1
 * @date 2022/9/7 21:27
 */
@Service
public class PrivilegeServiceImpl extends ServiceImpl<PrivilegeMapper, Privilege> implements PrivilegeService {

    @Autowired
    private PrivilegeService privilegeService;

    /**
     * 依据 role_id 在 table_privilege表中查询 menu_id
     * @param roleIdList
     * @return
     */
    @Override
    public List<List<Integer>> FindMenuIdByRoleId(List<Integer> roleIdList) {
        List<List<Integer>> menuList = new ArrayList<>();  //存储父菜单和子菜单  例如:[[1,2],[100,101,111,112]]
        List<Integer> parentList = new ArrayList<>();      // 父菜单 [1,2]
        List<Integer> childrenList = new ArrayList<>();    // 子菜单 [100,101]
        for (Integer roleId : roleIdList) { //遍历传过来的roleIdList 处理每一个roleId
            LambdaQueryWrapper<Privilege> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Privilege::getRoleId, roleId);
            List<Privilege> privilegeList = privilegeService.list(queryWrapper);

            for (Privilege privilege : privilegeList) {
                Integer menuId = privilege.getMenuId(); //获得菜单id
                //假设父菜单在十个以内，将menuId<10的放入父菜单列表中
                if (menuId <= 10) {
                    parentList.add(menuId);
                }
                //其余都放到子菜单列表中
                else{
                    childrenList.add(menuId);
                }
            }
            menuList.add(parentList);
            menuList.add(childrenList);
        }
        return menuList;
    }
}
